﻿using Magic.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using Magic.Application.Entity;
namespace Magic.Application
{
    /// <summary>
    /// 测试表服务
    /// </summary>
    [ApiDescriptionSettings(Name = "Test", Order = 100)]
    public class TestService : ITestService, IDynamicApiController, ITransient
    {
        private readonly ISqlSugarRepository<Test> _rep;

        public TestService(ISqlSugarRepository<Test> rep)
        {
            _rep = rep;
        }

        /// <summary>
        /// 分页查询测试表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Test/page")]
        public async Task<dynamic> Page([FromQuery] TestInput input)
        {
            var entities = await _rep.Context.Queryable<Test>()
                                     .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name == input.Name)
                                     .ToPagedListAsync(input.PageNo, input.PageSize);
            return XnPageResult<Test>.SqlSugarPageResult(entities);
        }

        /// <summary>
        /// 增加测试表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Test/add")]
        public async Task Add(AddTestInput input)
        {
            var entity = input.Adapt<Test>();
            await _rep.InsertAsync(entity);
        }

        /// <summary>
        /// 删除测试表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Test/delete")]
        public async Task Delete(DeleteTestInput input)
        {
            var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
            await _rep.DeleteAsync(entity);
        }

        /// <summary>
        /// 更新测试表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Test/edit")]
        public async Task Update(UpdateTestInput input)
        {
            var entity = input.Adapt<Test>();
            await _rep.Context.Updateable(entity).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommandAsync();
        }

        /// <summary>
        /// 获取测试表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Test/detail")]
        public async Task<Test> Get([FromQuery] QueryeTestInput input)
        {
            return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
        }

        /// <summary>
        /// 获取测试表列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Test/list")]
        public async Task<dynamic> List([FromQuery] TestInput input)
        {
            return await _rep.ToListAsync();
        }
    }
}
